﻿using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WgFrame.Net;

public class GameNetwork
{
    private static WgClient Client;

    public static Player Player;

    public static string ClientGuId;

    public static bool IsConnected { get; set; }

    static GameNetwork()
    {
        Client = new WgClient();
        Client.Peer.DisconnectTimeout = ConfigUtil.DisconnectTimeout;
        Client.Peer.TimePingInterval = ConfigUtil.TimePingInterval;
        Timers.inst.Add(1, 0, OnTime);
    }

    public static void Connect(string serverAddress, string appId)
    {
        if (Client == null || IsConnected)
        {
            return;
        }
        Client.Connect(serverAddress, appId, null);
    }

    public static void Connect()
    {
        Connect(ConfigUtil.ServerAddress, ConfigUtil.AppId);
    }

    public static void Disconnect()
    {
        if (Client == null)
        {
            return;
        }

        Client.Disconnect();
    }

    public static void Service()
    {
        if (Client == null)
        {
            return;
        }
        Client.Service();
    }

    public static void SendOperation(short operationCode, Dictionary<byte, object> operationParameters, SendOptions sendOptions)
    {
        if (Client == null)
        {
            return;
        }
        sendOptions.Encrypt = true;
        Client.Peer.SendOperation(operationCode, operationParameters, sendOptions);
    }

    public static void AddCallbackTarget(object target)
    {
        if (Client == null)
        {
            return;
        }

        Client.AddCallbackTarget(target);
    }

    public static void RemoveCallbackTarget(object target)
    {
        if (Client == null)
        {
            return;
        }

        Client.RemoveCallbackTarget(target);
    }

    private static void OnTime(object param)
    {
        if (Player != null)
        {
            if (Player.BaseInfo.ZhaoMuRemainTime > 0)
            {
                Player.BaseInfo.ZhaoMuRemainTime--;
                MsgListenerMgr.FireMsg(MsgListenerType.UpdateZhaoMuTime);
                if (Player.BaseInfo.ZhaoMuRemainTime == 0)
                {
                    MsgListenerMgr.FireMsg(MsgListenerType.ZhaoMuTimeZero);
                }
            }
        }
    }

}